<?php

/*
 * Following code will list all the companies
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();
$con = $db->getConnection();

// get all companies from companies table
$result = mysqli_query($con, "SELECT * FROM company where published = 1 ") or die(mysqli_error($con));

// check for empty result
if (mysqli_num_rows($result) > 0) {
    // looping through all results
    // companies node
    $response["companies"] = array();
    
    while ($row = mysqli_fetch_array($result)) {
        // temp user array
        $company = array();
        $company["id"] = $row["id"];
        $company["name"] = $row["name"];
        $company["owner"] = $row["owner"];
        $company["address"] = $row["address"];
        $company["city"] = $row["city"];
        $company["phone"] = $row["phone"];		
		$company["category_id"] = $row["category_id"];
		$company["zipcode"] = $row["zipcode"];		
		$company["lat"] = $row["lat"];		
		$company["lng"] = $row["lng"];
		$company["published"] = $row["published"];	
		$company["picture"] = $row["picture"];			

        // push single company into final response array
        array_push($response["companies"], $company);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no companies found
    $response["success"] = 0;
    $response["message"] = "No companies found";

    // echo no users JSON
    echo json_encode($response);
}
?>
